package Chapter10;

import java.io.*;

public class Demo02 {
    //使用java的输入和输出流将目录中的log.txt文件按行读出，每读出一行就顺序添加行号，并写入到log2.txt目录中的log2.txt文件中
    public static void main(String[] args) {
        String inpath= "src/Chapter10/log.txt";
        String outPath = "src/Chapter10/log2.txt";
        try (BufferedReader bufferedReader = new BufferedReader(new FileReader(inpath));
             BufferedWriter bufferedWriter= new BufferedWriter(new FileWriter(outPath))) {
            String line;
            int lineNumber = 1;
            while ((line = bufferedReader.readLine()) != null) {
                String lineWithNumber = lineNumber + ": " + line;
                bufferedWriter.write(lineWithNumber);
                bufferedWriter.newLine();
                lineNumber++;
            }
            System.out.println("File processed successfully.");
        } catch (IOException e) {
            System.out.println("An error occurred: " + e.getMessage());
        }
    }
}
